/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Resources1;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author escabia
 */
public class Settings {
    private static final ArrayList<String> playersBJ = new ArrayList<>();
    private static final ArrayList<String> playersUNO = new ArrayList<>();
    private static boolean leido = false;
    private static final ArrayList<String> botsUNO = new ArrayList<>();
    private static final ArrayList<String> botsBJ = new ArrayList<>();
    
    private static void leer() throws Exception{
        /** Lee fichero de configuración **/
        try {
            File fXmlFile = new File("src/Resources1/properties.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            
            /** Extrae variables importantes **/
            
            NodeList blackJack = doc.getElementsByTagName("blackjack");
            NodeList playersBlackJack = blackJack.item(0).getChildNodes();
            
            //Eliminamos los nodos de texto
            for (int i = 0; i<playersBlackJack.getLength(); i++){
                Node n = playersBlackJack.item(i);
                if (n.getNodeType() == Node.TEXT_NODE){
                    n.getParentNode().removeChild(n);
                }
            }
            for (int temp = 0; temp < playersBlackJack.getLength(); temp++) {
                Node nNode = playersBlackJack.item(temp);
                if (playersBlackJack.item(temp).getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    if (eElement.getNodeName().equals("player")){
                        playersBJ.add(eElement.getElementsByTagName("name").item(0).getTextContent());
                    }
                    else{
                        botsBJ.add(eElement.getElementsByTagName("name").item(0).getTextContent());
                    }
                }
            }
            if ((playersBJ.size() + botsBJ.size() > 7) || (playersBJ.size() + botsBJ.size() < 1)){
                throw new Exception("Incorrect players for Blackjack; modify your properties.xml");
            }
            NodeList UNO = doc.getElementsByTagName("uno");
            NodeList playersUnoXml = UNO.item(0).getChildNodes();
            for (int i = 0; i<playersUnoXml.getLength(); i++){
                Node n = playersUnoXml.item(i);
                if (n.getNodeType() == Node.TEXT_NODE){
                    n.getParentNode().removeChild(n);
                }
            }
            for (int temp = 0; temp < playersUnoXml.getLength(); temp++) {
                Node nNode = playersUnoXml.item(temp);
                if (playersUnoXml.item(temp).getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    if (eElement.getNodeName().equals("player")){
                        playersUNO.add(eElement.getElementsByTagName("name").item(0).getTextContent());
                    }
                    else{
                        botsUNO.add(eElement.getElementsByTagName("name").item(0).getTextContent());
                    }
                }
            }
            if ((playersUNO.size() + botsUNO.size() > 12) || (playersUNO.size() + botsUNO.size() < 2)){
                throw new Exception ("Incorrect players for UNO; modify your properties.xml");
            }
        } catch (ParserConfigurationException | SAXException | IOException | DOMException e) {
            System.out.println(e.toString());
        }
        
    }
    
    /**
     * Method that returns an array with the name of the BlackJack players read from properties.xml
     * @return An arrayList containing the names of the BlackJack players
     * @throws Exception if the number of players in properties.xml is incorrect
     */
    public static ArrayList<String> getPlayersBJ() throws Exception{
        if (!leido){
            leer();
            leido = true;
        }
        
        return Settings.playersBJ;
    }
    /**
     * Method that returns an array with the name of the UNO players read from properties.xml
     * @return An arrayList containing the names of the UNO players
     * @throws Exception if the number of players in properties.xml is incorrect
     */
    public static ArrayList<String> getPlayersUNO() throws Exception{
        if (!leido){
            leer();
            leido = true;
        }
        return Settings.playersUNO;
    }
    /**
     * Method that returns an array with the name of the BlackJack bots read from properties.xml
     * @return An arrayList containing the names of the BlackJack bots
     * @throws Exception if the number of players in properties.xml is incorrect
     */
    public static ArrayList<String> getBotsBJ() throws Exception{
        if (!leido){
            leer();
            leido = true;
        }
        return botsBJ;
    }
    /**
     * Method that returns an array with the name of the UNO bots read from properties.xml
     * @return An arrayList containing the names of the UNO bots
     * @throws Exception if the number of players in properties.xml is incorrect
     */
    public static ArrayList<String> getBotsUNO() throws Exception{
        if (!leido){
            leer();
            leido = true;
        }
        return botsUNO;
    }
}
